package com.api.nosession;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.component.domain.MChannel;
import com.component.domain.Note;
import com.component.service.MChannelService;
import com.component.service.NoteService;
import com.system.common.Result;
import com.system.security.IsSecurity;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Classname NoteNoSessionApi
 * @Description TODO
 * @Date 2023/9/28 15:39
 * @Created by XiongXiong
 * @Author: X.I.O
 */
@IsSecurity(isSecurity = false)
@RestController
@RequestMapping("/no/note")
public class NoteNoSessionApi {

    @Autowired
    private NoteService noteService;

    @Autowired
    private MChannelService mChannelService;

    @ApiModelProperty(value = "智能搜索词")
    @GetMapping("keyword")
    public JSONObject keywordList(@RequestParam(value = "keyword",required = false)String keyword){
        List<Map<String,Object>> mapList=noteService.searchKeywordList(keyword);
        return Result.successInfo(mapList);
    }

    @ApiModelProperty("内容分页")
    @GetMapping("page")
    public JSONObject notePageController(@Min(1) @RequestParam(value = "page",defaultValue = "1")
                                         @ApiParam("页码") Integer page,
                                         @Max(20)@RequestParam(value = "size",defaultValue = "10")
                                         @ApiParam("条数")Integer size,
                                         @RequestParam(value = "channelpath",required = false)String channelpath,
                                         @RequestParam(value = "title",required = false)String title,
                                         @RequestParam(value = "tag",required = false)String tag,
                                         @RequestParam(value = "createdate",required = false)String date,
                                         @ApiParam("关键字")@RequestParam(value = "keyword",required = false)String keyword){

        String channelId=null;
        if(channelpath!=null && !"".equals(channelpath)){
            MChannel mChannel=mChannelService.getMChannelByPath(channelpath);
            if(mChannel != null){
                channelId = mChannel.getId().toString();
            }
        }
        Map<String,Object> query=new HashMap<>();
        if(org.apache.commons.lang3.StringUtils.isNotBlank(tag)){
            query.put("tag",tag);
        }
        if(org.apache.commons.lang3.StringUtils.isNotBlank(title)){
            query.put("title",title);
        }
        if(org.apache.commons.lang3.StringUtils.isNotBlank(date)){
            query.put("date", date);
        }
        IPage<Note> page1=noteService.notePageListByKeyWord(page,size,channelId,keyword,query,false);
        return Result.successInfo("操作成功",page1);
    }
}
